package com.example.thirdstage_project.controller;

import com.example.thirdstage_project.common.Code;
import com.example.thirdstage_project.common.Result;
import com.example.thirdstage_project.entity.Useradmin;
import com.example.thirdstage_project.service.UseradminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 86158
 * @version 1.0
 * Create by 2022/11/28 21:07
 */

@RestController
@RequestMapping("/useradmins")
public class UserAdminController {
    @Autowired
    private UseradminService useradminService;

    /**
     * 查询所有
     */
    @GetMapping
    public Result selectAll(){
        List<Useradmin> list = useradminService.list();
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败";
        return new Result(code,list,msg);
    }

    /**
     * 根据id删除
     *
     *
     */
    @DeleteMapping("/{aid}")
    public Result delete(@PathVariable Integer aid){
        boolean delete = useradminService.removeById(aid);
        return new Result(delete ? Code.DELETE_OK : Code.DELETE_ERR,delete);
    }

    /**
     * 添加
     */
    @PostMapping
    public Result save(@RequestBody Useradmin useradmin){
        boolean save = useradminService.save(useradmin);
        return new Result(save ? Code.SAVE_OK : Code.SAVE_ERR,save);
    }



}
